local proto = require "proto"

function net_test()

	local server = network.create_sync_node()
	local client = network.create_sync_node()

	local test_action
	test_action = {
		create = function()
			return proto.clone(test_action)
		end,
		archive = function(self, bs)
			print("Read/write action")
			if bs.inp then self.data = {} end

			for i=1,200 do
				self.data[i] = bs.int(self.data[i])
			end
		end,
		apply = function(self, world)
			print("Applied action with data")

			for i=1,200 do
				assert(self.data[i] == i)
			end

			world.ok = true
		end
	}

	local function init(n)
		n:action(test_action)
	end

	init(server)
	init(client)

	local test_action_inst = test_action.create()
	test_action_inst.data = {}

	for i=1,200 do
		test_action_inst.data[i] = i
	end
	
	server:host(31337)
	local server_link = client:connect("localhost", 31337)

	-- TODO: This request won't work until the state has arrived
	client:request(test_action_inst)

	local ended_frame = false
	

	print("Hosting")

	repeat
		server:update()
		client:update()

		if server.ok and not ended_frame then
			server:attempt_end_base_frame()
			ended_frame = true
		end
		client:attempt_end_base_frame()
	until client.ok and server.ok

	print(client:latency(server_link))

	print "Success"
end